var windowName = "index";
var Page
$(function(){
    // onWindowShow(function () {
    // })
    // onWindowHide(function () {
    // })
    // console.log("index page is OK");
    var OrderList = new List();
    Page = createVM("#Page",{
        //页面数据
        docked: false,
        open: false,
        position: 'left',
        shift: 'btn3',
        cur: 'btn3',
        tips:"",
        pics:"",
        phone:"15265461237",
        orders:[],
    },{
        //页面方法
        "init":function () {
            // showLoading()
            var _this = this
            OrderList.setUrl("/po/getOrder").setCbFunc(function (res) {
                console.log(res);
                _this.orders = res;
            })
        }
        ,"change":function (val) {
            console.log(val);
            if (val == 'btn1') {
                // this.open = true
            }
            this.cur = val;
        },
        addPic:function (pic) {
            this.pics = pic
        }
        ,del:function (index) {
            console.log(index);
            this.pics = delIndex(index,this.pics)
        }
        ,submit:function () {
            $("#form").submit();
            // $.post("/po/createOrder",{
            //     phone:Page.phone,
            //     tips:Page.tips,
            //     pics:Page.pics,
            // },function (res) {
            //     console.log(res);
            // })
        }
        ,getOrder:function () {
            OrderList.getList();
        }
        ,reload:function () {
            OrderList.reload();
        }
        ,print:function () {
            doPrint("#print")
        }
        ,savePdf:function (id) {
            doPrint("#" + id)
        }
        , goback: function () {
            window.history.back(-1)
        }
    })

    Page.init()


    function delIndex (picIndex,pics){
        let update = false;
        if (pics === undefined){
            pics = this.uploadedPath
            update= true
        }
        var newData = []
        // var newViewData = []
        pics.map(function (data,index) {
            console.log(index);
            console.log(picIndex);
            if (picIndex === index) {return;}
            newData.push(data)
            // newViewData.push(baseUrl + data)
        })
        if (update) {
            this.uploadedPath = newData
        }
        return newData
    }



    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '/MyWebPicUpload' //上传接口
            ,field:"Filedata"
            ,done: function(res){
                //上传完毕回调
                console.log(res);
                if (res.s === 1) {
                    Page.addPic(res.d)
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });


    (function($){"use strict";function getjQueryObject(string){var jqObj=$("");try{jqObj=$(string).clone();}catch(e){jqObj=$("<span />").html(string);}
        return jqObj;}
        function printFrame(frameWindow,content,options){var def=$.Deferred();try{frameWindow=frameWindow.contentWindow||frameWindow.contentDocument||frameWindow;var wdoc=frameWindow.document||frameWindow.contentDocument||frameWindow;if(options.doctype){wdoc.write(options.doctype);}
            wdoc.write(content);wdoc.close();var printed=false;var callPrint=function(){if(printed){return;}
                frameWindow.focus();try{if(!frameWindow.document.execCommand('print',false,null)){frameWindow.print();}
                    $('body').focus();}catch(e){frameWindow.print();}
                frameWindow.close();printed=true;def.resolve();}
            $(frameWindow).on("load",callPrint);setTimeout(callPrint,options.timeout);}catch(err){def.reject(err);}
            return def;}
        function printContentInIFrame(content,options){var $iframe=$(options.iframe+"");var iframeCount=$iframe.length;if(iframeCount===0){$iframe=$('<iframe height="0" width="0" border="0" wmode="Opaque"/>').prependTo('body').css({"position":"absolute","top":-999,"left":-999});}
            var frameWindow=$iframe.get(0);return printFrame(frameWindow,content,options).done(function(){setTimeout(function(){if(iframeCount===0){$iframe.remove();}},1000);}).fail(function(err){console.error("Failed to print from iframe",err);printContentInNewWindow(content,options);}).always(function(){try{options.deferred.resolve();}catch(err){console.warn('Error notifying deferred',err);}});}
        function printContentInNewWindow(content,options){var frameWindow=window.open();return printFrame(frameWindow,content,options).always(function(){try{options.deferred.resolve();}catch(err){console.warn('Error notifying deferred',err);}});}
        function isNode(o){return!!(typeof Node==="object"?o instanceof Node:o&&typeof o==="object"&&typeof o.nodeType==="number"&&typeof o.nodeName==="string");}
        $.print=$.fn.print=function(){var options,$this,self=this;if(self instanceof $){self=self.get(0);}
            if(isNode(self)){$this=$(self);if(arguments.length>0){options=arguments[0];}}else{if(arguments.length>0){$this=$(arguments[0]);if(isNode($this[0])){if(arguments.length>1){options=arguments[1];}}else{options=arguments[0];$this=$("html");}}else{$this=$("html");}}
            var defaults={globalStyles:true,mediaPrint:false,stylesheet:null,noPrintSelector:".no-print",iframe:true,append:null,prepend:null,manuallyCopyFormValues:true,deferred:$.Deferred(),timeout:750,title:null,doctype:'<!doctype html>'};options=$.extend({},defaults,(options||{}));var $styles=$("");if(options.globalStyles){$styles=$("style, link, meta, base, title");}else if(options.mediaPrint){$styles=$("link[media=print]");}
            if(options.stylesheet){$styles=$.merge($styles,$('<link rel="stylesheet" href="'+options.stylesheet+'">'));}
            var copy=$this.clone();copy=$("<span/>").append(copy);copy.find(options.noPrintSelector).remove();copy.append($styles.clone());if(options.title){var title=$("title",copy);if(title.length===0){title=$("<title />");copy.append(title);}
                title.text(options.title);}
            copy.append(getjQueryObject(options.append));copy.prepend(getjQueryObject(options.prepend));if(options.manuallyCopyFormValues){copy.find("input").each(function(){var $field=$(this);if($field.is("[type='radio']")||$field.is("[type='checkbox']")){if($field.prop("checked")){$field.attr("checked","checked");}}else{$field.attr("value",$field.val());}});copy.find("select").each(function(){var $field=$(this);$field.find(":selected").attr("selected","selected");});copy.find("textarea").each(function(){var $field=$(this);$field.text($field.val());});}
            var content=copy.html();try{options.deferred.notify('generated_markup',content,copy);}catch(err){console.warn('Error notifying deferred',err);}
            copy.remove();if(options.iframe){try{printContentInIFrame(content,options);}catch(e){console.error("Failed to print from iframe",e.stack,e.message);printContentInNewWindow(content,options);}}else{printContentInNewWindow(content,options);}
            return this;};})(jQuery);
    function doPrint(id) {
        jQuery(id).print()
    }


})

